﻿using Microsoft.EntityFrameworkCore;
using PowerHub.MES.Common;
using PowerHub.MES.DTO.BasicData.DTO;
using PowerHub.MES.DTO.BasicData.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities;
using PowerHub.MES.Entities.BasicData;
using PowerHub.MES.IRepository.IBasic;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PowerHub.MES.Repository.Basic
{
    /// <summary>
    /// 角色菜单数据实现层
    /// </summary>
    public class BasicData_Roleinfo_MenuInfoRepository : BaseRepository<BasicData_Roleinfo_MenuInfo>, IBasicData_Roleinfo_MenuInfoRepository
    {
        private PowerHubMesContext _repositorySys;
        public BasicData_Roleinfo_MenuInfoRepository(PowerHubMesContext repositorySys) : base(repositorySys)
        {
            _repositorySys = repositorySys;
        }

        /// <summary>
        /// 绑定菜单
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateBasicData_Roleinfo_MenuInfoAsync(List<BasicData_Roleinfo_MenuInfo> createMenuInfos, List<BasicData_Roleinfo_MenuInfo> deleteMenuInfos, string roleinfoId)
        {
            var result=new OperationResult<bool>();
            var tran = _repositorySys.Database.BeginTransaction();

            try
            {
                //新增
                AddEntitieRange(createMenuInfos);

                //删除
                DeleteEntityRange(deleteMenuInfos);
                result.Data = true;

                tran.Commit(); //提交
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Data = false;
                tran.Rollback(); //回滚
            }

            return result;
        }

        /// <summary>
        /// 根据用户ID获取角色菜单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<string>> GetBasicData_Roleinfo_MenuInfoPageListAsync(QueryBasicData_Roleinfo_MenuInfoDTO query)
        {
            var result = GetEntitys().AsNoTracking()
                                            .Where(p => p.IsDel == 0)
                                            .Where(p => p.RoleinfoId.Equals(query.RoleinfoId))
                                            .Select(p=>p.MenuInfoId).ToList();
            return result;
        }
        
     
    }
}
